/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('AuditProblemCheckCtrl', ['$scope','AuditDictServices','WarningAddService','$http','$modal',
	                                         'CaseCommonService','CaseReviewResultSurveyService','WarningScreenService',
	                                             'CaseAuditProjectAddService','$state','BaseURL','Modal','FwaTabsetService','$stateParams','$sce',
	                                function($scope,AuditDictServices,WarningAddService,$http,$modal,
	                                		CaseCommonService,CaseReviewResultSurveyService,WarningScreenService,
	                                		CaseAuditProjectAddService,$state,baseUrl,Modal,fwaTabsetService,$stateParams,$sce){

		$scope.auditDictServices = AuditDictServices;
        AuditDictServices.initCodeListDatas();
        
        $scope.UrlParams = $stateParams;
		$scope.CaseAuditProjectAddService = CaseAuditProjectAddService;
		var sourcetab=[ {label:'事件处理',id:0,url:'views/hcpms/audit/problemCheck/caseAduitDealProblemEdit.html'},
		                {label:'事件处理',id:0,url:'views/hcpms/audit/problemCheck/caseAduitDealProblemView.html'}];
		
		var _bt ='<button type="button"  class="btn btn-blue btn-sm" ng-click="onlySave()" > 暂存 </button>'+
			'<button type="button"  class="btn btn-blue btn-sm" ng-click="submitApr()"  > 保存 </button> '+
			'<button type="button"  class="btn btn-blue btn-sm" ng-click="goBack()">返回</button>';
		
		var _bt3=' <button type="button"  class="btn btn-blue btn-sm" ng-click="returnApr()" > 撤回 </button> ' +
			'<button type="button"  class="btn btn-blue btn-sm" ng-click="goBack()">返回</button>';
		
		$scope.problemtab=[];

		 //事件对象
		 $scope.problemObj={};
		 
        $scope.tabClicks = function(tab) {
            $scope.selectedTabs = tab;
            $scope.tabIndex = tab.id;
        }
        
		$scope.tempStaffList=new Array(); //临时保存医务人员信息
		$scope.tempInsuranceList=new Array();//临时保存参保人信息
		
		$scope.enableEdit = true;	

		//初始化请求参数
		var params = function (){
			var problemNo = "";
			if($stateParams.problemCode && !angular.isUndefined($stateParams.problemCode) ){
				problemNo = $stateParams.problemCode;
				$scope.problemNo=problemNo;
				
			}
			var params = {
					problemNo:problemNo
			}
			return params;
		}
		
		
        /**
         * 初始化医疗机构数据和医护人员信息和参保人信息
         */
        $scope.medicalCode='';
        function initMedicalData(){
            var parameter=params();
            CaseCommonService.loadMedicalInfoList(parameter).success(function(response){
                $scope.medicalData2=response.medicalBean.medicalList;
                if($scope.medicalData2.length>0)
                {
                    if($scope.medicalData2[0].medicalCode)
                    {
                        $scope.medicalCode=$scope.medicalData2[0].medicalCode;
                    }
                }
                $scope.staffData2=response.staffBean.staffList;
                $scope.insuranceData2=response.insuranceBean.insuranceList;
                
                CaseCommonService.initStaffAndInsuranceTempList($scope);
				//更新当前页面数据
     		    WarningScreenService.findPointCutTempScose(
     				   WarningAddService.warningMarks,$stateParams.problemCode,'02',
     				   $scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);

            });
        }
		
		/**
		 * 初始化事件数据对象
		 */
        $scope.object={};
		function initProblemData(){
			var parameter=params();
			CaseCommonService.loadProblemData(parameter).success(function(response){
		    	 var obj=response.problemObj;
		    	 $scope.object=obj;	
		    	 if(obj.problemStatus==3){
		    		 $scope.problemtab.push(sourcetab[1]);
		    		 $scope.buttonSource = $sce.trustAsHtml(_bt3);
		    		 $scope.enableEdit = false;
		    		 $scope.medicalShow={add:false,deduction:false,del:false};
		    		 $scope.staffShow={add:false,deduction:false,del:false};
		    		 $scope.insuranceShow={add:false,deduction:false,del:false};
		    	 }else{
		    		 $scope.problemtab.push(sourcetab[0]);
		    		 $scope.buttonSource = $sce.trustAsHtml(_bt);
		    		 $scope.medicalShow={add:false,deduction:true,del:false};
		    		 $scope.staffShow={add:true,deduction:true,del:true};
		    		 $scope.insuranceShow={add:true,deduction:false,del:true};
		    	 }
		    	 $scope.tabClicks($scope.problemtab[0]);
			});
		}
		
		
	
		initProblemData();
		initMedicalData();
		
		
		
		$scope.auditObj={}; //保存审核意见和审核意见描述对象
		$scope.treatmentAdvice={}; //映射事件的对象
		
		$scope.aduitSurveyResult={}; //调查结果对象
		$scope.auditReviewResult={}; //调查复核结果对象
		$scope.showConfirmButton=false; //隐藏确定button
		$scope.showDispatchButton=true; //展现分配button
		$scope.selectList=[];
		
		/**
		 * 返回事件
		 */
		$scope.goBack = function(){
		   	WarningAddService.warningMarks = WarningAddService.warningMarkTemps;//初始化扣分参数
			$state.go('problemRegister.problemRegisterList');
		}
		
		/*****初始化事件处理和调查结果的两个tab页面的信息********/
		function initProblemTabAndSurveyTabInfo(){
			
			var parameter={problemNo:$stateParams.problemCode};
			
			CaseReviewResultSurveyService.loadProblemDealOpinion(parameter).success(function(response){
				  if(response.treatmentOpinion){
					  $scope.treatmentAdvice=response.treatmentOpinion;
				  }
				  $scope.auditOpinion=response.auditOpinion;
				  if(response.surveyOpinion){
					  
					  $scope.aduitSurveyResult={
								surveyMessage:response.surveyOpinion.opinionDesc, //调查详情
								surveyOpinion:response.surveyOpinion.opinion, //处理意见
								surveyOpinionMsg:response.surveyOpinion.opinion,
								opertionUsername:response.surveyOpinion.dealUserName, //操作人
								opertionDate:response.surveyOpinion.dealDate
						}; //调查结果对象
					  
				  }
				  
				  if(response.surveyAuditOpinion){
					  
					  
					  $scope.auditReviewResult={
								reviewResultMsg:response.surveyAuditOpinion.opinion, //复核意见
								reviewOpinionMsg:response.surveyAuditOpinion.opinionDesc, //复核意见说明
								reviewUsername:response.surveyAuditOpinion.dealUserName, //复核人
								reviewDate:response.surveyAuditOpinion.dealDate,
								reviewResult:response.surveyAuditOpinion.opinion	
						}; //调查复核结果对象
					  
					  
				  }
				 
					if($scope.auditReviewResult.reviewResult=='0'){ //复核人保留意见则 需要重新调查
						$scope.selectList=[{label:'重新调查',code:'0'}];
					}else{
						$scope.selectList=[{label:'同意',code:'1'},{label:'重新调查',code:'0'}];
					}
			});
		}
		

		/******审核下拉框改变事件*********/
		$scope.selectChangeFun=function(){
			var selectOpinionValue=$scope.auditObj.opinion; //选择的审核意见值
			
			//A岗 不需要稽核,B岗位同意,审核岗位也同意  则分配按钮隐藏 ，确定按钮展现 流程结束
			if($scope.aduitSurveyResult.surveyOpinion=='0' && $scope.auditReviewResult.reviewResult=='1'&& selectOpinionValue=='1'){
				
				$scope.showConfirmButton=true; //隐藏确定button
				$scope.showDispatchButton=false; //展现分配button
			}else{
				$scope.showConfirmButton=false; //隐藏确定button
				$scope.showDispatchButton=true; //展现分配button
			}
			
		}
		
		/****************一下处理 事件处理******************/
		
		initProblemTabAndSurveyTabInfo(); //初始化事件处理Tab和调查结果tab页面

        function check()
        {
        	if($scope.treatmentAdvice==null){
        		$scope.treatmentAdvice = {};
        	}
            if(!$scope.treatmentAdvice.opinion)
            {
                Modal.alert('请填写处理意见!');
                return false;
            } else if(!$scope.treatmentAdvice.opinionDesc)
            {
                Modal.alert('请填写调查详情！');
                return false;
            } 
            return true;
        }        
        
        /**
         * 暂存
         */
        $scope.loading = false;
        $scope.onlySave = function(){
        	if(!check()){
        		return;
        	}
        	$scope.loading = !$scope.loading;
        	var opinionDTO = new Object();
        	opinionDTO.problemNo=$stateParams.problemCode;
        	opinionDTO.opinion = $scope.treatmentAdvice.opinion;
        	opinionDTO.opinionDesc = $scope.treatmentAdvice.opinionDesc;
        	opinionDTO.onlySave = 1;
        	//从初审变更到待审核
        	opinionDTO.statusAfter = 3;
        	//意见信息调查初审
        	opinionDTO.opinionTypeCurrent = 1;
            opinionDTO.warnMarkTempDetails=WarningAddService.warningMarks;
        	CaseCommonService.approvementProblemOpinion(opinionDTO).success(function(data){
        		$scope.saveStaffAndInsuarnceInfo();
        		$scope.loading = !$scope.loading;
                Modal.alert(data.msg);
                WarningAddService.warningMarks = [];//初始化扣分参数
                
                
            	//更新当前页面数据
     		    WarningScreenService.findPointCutTempScose(
     				   WarningAddService.warningMarks,$stateParams.problemCode,'02',
     				   $scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);
            });
        }
        
        /**
         * 查看事件对象的案件来源对话框信息
         */
        $scope.$on('fwaAuditLeft.showModal',function(){
     	   $scope.problemObj=$scope.object;
     	   CaseCommonService.showProblemFromSourceDialog($scope);
        });
        
        /**
         * 提交
         */
        $scope.submitApr = function(){
        	if(!check()){
        		return;
        	}
        	$scope.loading = !$scope.loading;
        	var opinionDTO = new Object();
        	opinionDTO.problemNo=$stateParams.problemCode;
        	opinionDTO.opinion = $scope.treatmentAdvice.opinion;
        	opinionDTO.opinionDesc = $scope.treatmentAdvice.opinionDesc;
        	opinionDTO.onlySave = 0;
        	//从初审变更到待审核
        	opinionDTO.statusAfter = 3;
        	//意见信息调查初审
        	opinionDTO.opinionTypeCurrent = 1;
            opinionDTO.warnMarkTempDetails=WarningAddService.warningMarks;
        	CaseCommonService.approvementProblemOpinion(opinionDTO).success(function(data){
        		$scope.saveReallyStaffAndInsuarnceInfo();
        		$scope.loading = !$scope.loading;
                Modal.alert(data.msg);
                $scope.goBack();
            });
        	
        }
        /**
         * 医疗机构扣分事件
         */
        $scope.$on('fwaInvolve.medicalDeduction',function(e,v){
            $scope.marking (1,v, v.medicalCode, v.orgType);
        });
        /**
         * 医务人员扣分事件
         */
        $scope.$on('fwaInvolve.staffDeduction',function(e,v){
            $scope.marking(2,v,$scope.medicalCode,'');
        });
        /**
         * 调新增医务人员的回调函数
         *  parameter={
            * taskCode:'',
            * taskType:'',1事件,2案件
            * targetType:'',对象类型(1-医疗机构2-医生3-参保人)
            * targetCode:''
            * }
         */
        $scope.callStaffFun=function(array){
        	CaseCommonService.addTempStaffList(array,$scope);
        }
        /**
         * 调新增参保人员的回调函数
         */
        $scope.callInsuranceFun=function(array){
        	CaseCommonService.addTempInsuranceListInfo(array,$scope);
        }
        
        /**
         * 点击医疗机构量事件
         * 
         */
        $scope.$on('fwaInvolve.medicalCountClick',function(){
     	   CaseCommonService.viewMedicalDetail($scope);
        });
        
        /**
         * 点击医务人员总数量事件
         */
        $scope.$on('fwaInvolve.staffCountClick',function(){
        	CaseCommonService.viewStaffList($scope);
        });
        
        /**
         * 点击参保人总数量事件
         */
        $scope.$on('fwaInvolve.insuranceCountClick',function(){
        	CaseCommonService.viewInsurList($scope);
        });

        /**
         * 新增医护人员事件
         */
        $scope.$on('fwaInvolve.staffAdd',function(){
            CaseCommonService.addStaffFun($scope);
        });

        /**
         * 新增参保人员事件
         */
        $scope.$on('fwaInvolve.insuranceAdd',function(){
            CaseCommonService.addInsuranceFun($scope);
        });

        /**
         * 医务人员删除
         */
        $scope.$on('fwaInvolve.staffDel',function(e,data){
        	Modal.confirm('确认删除医务人员'+data.name+'吗？',function(){
				WarningScreenService.deleteMark(WarningAddService.warningMarks,2,data);//mark:1.机构，2.医务人员，3.参保人
				 CaseCommonService.deleteTempStaffList(data,$scope);
			});
        });

        /**
         * 参保人员删除
         */
        $scope.$on('fwaInvolve.insuranceDel',function(e,data){
        	 Modal.confirm('确认删除参保人员'+data.name+'吗？',function(){
 				WarningScreenService.deleteMark(WarningAddService.warningMarks,3,data);//mark:1.机构，2.医务人员，3.参保人
 				 CaseCommonService.deleteTempInsuranceInfoList(data,$scope);
 			});
        });
        
        /**
 	    * 暂存的时候对医务人员进行的保存
 	    */
        function saveStaffListInfo(){
        	CaseCommonService.saveTempStaffListInfo($scope,'1','2',$stateParams.problemCode);
        }
        
        /**
 	    * 暂存的时候对参保人员进行的保存
 	    */
        function saveInsuranceInfo(){
     	   CaseCommonService.saveTempInsuranceListInfo($scope,'1','3',$stateParams.problemCode);
        }
        
       /**
        * 对医务人员和参保人员的暂存信息保存
        */
        $scope.saveStaffAndInsuarnceInfo=function(){
    	   $scope.saveType="temp";
    	   saveStaffListInfo();
    	   saveInsuranceInfo();
        }
       
       
       /**
        * 对医务人员和参保人员的真实数据信息保存
        */
        $scope.saveReallyStaffAndInsuarnceInfo=function(){
    	   $scope.saveType="save";
    	   saveStaffListInfo();
    	   saveInsuranceInfo();
        }

      //扣分 mark：1.机构扣分，2.医务人员扣分，3参保人员扣分,data:数据集合
        $scope.marking = function(mark,data,medicalCode,orgType){
            var medicalCodeTemp=null;
            if(medicalCode&&!angular.isUndefined(medicalCode)){
                medicalCodeTemp = medicalCode;
            }
            if(mark != 1 && medicalCodeTemp == null){
                Modal.alert("请先增加医疗机构信息！");
                return null;
            }else{
                medicalCode = medicalCodeTemp;
            }
            markData(mark,data,medicalCode,orgType);//扣分数据参数加载
            var modalInstance = $modal.open({
                templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningDeductionDialog_view.html',
                backdrop:'static',
                size:'sm',
                scope: $scope,
    //					  scope:scope,
                controller: 'WarningDeductionDialogCtrl'
            });
            //接收返回数据
            modalInstance.result.then(function(item) {
                if (!WarningAddService.warningMarks) {
                    WarningAddService.warningMarks = [];
                }
                WarningAddService.warningMarks = item;
                //更新当前页面数据
     		   	WarningScreenService.findPointCutTempScose(
     		   		item,$stateParams.problemCode,'02',
     		   	$scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);
            });
        }
        //扣分数据参数加载
        var markData = function(mark,data,medicalCode,orgType){
            WarningAddService.mark = mark;
            WarningAddService.warningMarkData = {
                orgType:orgType,//0.机构，1.药店
                medicalCode:medicalCode,//medicalCode:所属机构编号
                code:data.selfCode,//code:编号
                name:data.name,//name:名称
                caseType:'02',//案件类型caseType：01.预警,02.稽核
                caseNo: $stateParams.problemCode,//事件编号
                happenDate: $scope.object.auditDate,//稽核时间
                punishDateStart: $scope.object.periodBeginDate,// 稽核开始日期
                punishDateEnd: $scope.object.periodEndDate,// 稽核结束日期
                warningMarks:WarningAddService.warningMarks
            };
        }
        
        
        /**
 	    * 撤回
 	    */  
        $scope.returnApr = function (){
     	  $scope.loading = !$scope.loading;
       	 var premer={
       			 code:$stateParams.problemCode,
       			 status:"3",
       			 type:"01"
       	 }
           CaseCommonService.caseRollBack(premer).success(
 	           function(response, status, headers, config){
 	        	   $scope.loading = !$scope.loading;
 	        	   Modal.alert(response.result.message);
 	        	   $scope.goBack();
 	            });
       	 
        }
    }]);
})();
